#include "stdafx.h"
#include "DrawGLScene.h"


extern CGlobe g_global;
extern cWglARTK		g_wglARTK;

extern cPMDModel	g_clPMDModel;
extern cVMDMotion	g_DanceMotion;


int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
{ 
	glClear( GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );

	glDisable( GL_CULL_FACE );
	glDisable( GL_ALPHA_TEST );
	glDisable( GL_BLEND );

	g_wglARTK.display();
	g_wglARTK.capNextImage();


	glEnable( GL_CULL_FACE );
	glEnable( GL_ALPHA_TEST );
	glEnable( GL_BLEND );

	if( g_wglARTK.isMarkerFound() )
	{
		double	dMat[16];

		glMatrixMode( GL_PROJECTION );
		g_wglARTK.getProjectionMat( dMat );
		glLoadMatrixd( dMat );

		glMatrixMode( GL_MODELVIEW );
		glLoadIdentity();

		glPushMatrix();

		g_wglARTK.getModelViewMat( dMat );
		glLoadMatrixd( dMat );

		glScalef( 1.0f, 1.0f, -1.0f );	
		g_clPMDModel.render();

		glDisable( GL_CULL_FACE );
		glDisable( GL_TEXTURE_2D );
		glDisable( GL_LIGHTING );

		glEnable( GL_STENCIL_TEST );
		glStencilFunc( GL_ALWAYS, 1, ~0 );
		glStencilOp( GL_REPLACE, GL_KEEP, GL_REPLACE );

		glColorMask( 0, 0, 0, 0 );
		glDepthMask( 0 );

		glMultMatrixf( (const float *)g_global.g_matPlanarProjection );
		g_clPMDModel.renderForShadow();		

		glColorMask( 1, 1, 1, 1 );

		float	fWndW = g_global.windowWidth,
			fWndH = g_global.windowHeight;

		glStencilFunc( GL_EQUAL, 1, ~0);
		glStencilOp( GL_KEEP, GL_KEEP ,GL_KEEP );

		glDisable( GL_DEPTH_TEST );

		glMatrixMode( GL_PROJECTION );
		glPushMatrix();
		glLoadIdentity();
		gluOrtho2D( 0.0f, fWndW, 0.0f, fWndH );

		glMatrixMode( GL_MODELVIEW );
		glPushMatrix();
		glLoadIdentity();

		glColor4f( 0.2f, 0.2f, 0.2f, 0.5f );

		glBegin( GL_TRIANGLE_FAN );
		glVertex2f(  0.0f, fWndH );
		glVertex2f( fWndW, fWndH );
		glVertex2f( fWndW,  0.0f );
		glVertex2f(  0.0f,  0.0f );
		glEnd();

		glMatrixMode( GL_PROJECTION );
		glPopMatrix();
		glMatrixMode( GL_MODELVIEW );
		glPopMatrix();

		glDepthMask( 1 );
		glEnable( GL_LIGHTING );
		glEnable( GL_DEPTH_TEST );
		glDisable( GL_STENCIL_TEST );

		glPopMatrix();


		glBegin(GL_LINE);
			glVertex3f(g_global.wxOri, g_global.wyOri, g_global.wzOri);
			glVertex3f(g_global.wxDir, g_global.wyDir, g_global.wzDir);
		glEnd();
	}




	return TRUE;										// Everything Went OK
}